﻿using CoreShop.Library;
using SqlSugar;
using Sunny.UI;
using System;
using System.Runtime.InteropServices.ComTypes;

namespace CoreShop.App
{
    public partial class UIDateTimeRange : UIUserControl
    {
        public UIDateTimeRange()
        {
            InitializeComponent();
            dateselect.SelectedIndex = 0;
        }

        public string DateTimeStar
        {
            get => startDate.Text;
            set => startDate.Text = value;
        }

        public string DateTimeEnd
        {
            get => endDate.Text;
            set => endDate.Text = value;
        }

        private void dateselect_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            DateTime dtStar = DateTime.Now;
            DateTime dtEnd = DateTime.Now;
            switch (dateselect.SelectedIndex)
            {
                case 1://昨天
                    dtStar = dt.AddDays(-1);
                    dtEnd = dt.AddDays(-1);
                    break;
                case 2://本周
                    dtStar = dt.AddDays(1 - CommFunc.ConvertToInt32(dt.DayOfWeek.ToString("d")));
                    dtEnd = dtStar.AddDays(6);  //本周周日
                    break;
                case 3://本月
                    dtStar = dt.AddDays(1 - dt.Day);  //本月月初
                    dtEnd = dtStar.AddMonths(1).AddDays(-1);  //本月月末
                    break;
                case 4://本年
                    dtStar = CommFunc.ConvertToDateTime(dt.ToString("yyyy-01-01"));
                    dtEnd = CommFunc.ConvertToDateTime(dt.ToString("yyyy-01-01")).AddYears(1).AddDays(-1);
                    break;
            }
            startDate.Text = CommFunc.ConvertToDateTime(dtStar.ToString("yyyy-MM-dd") + " 00:00:00").ToString("yyyy-MM-dd HH:mm:ss");
            endDate.Text = CommFunc.ConvertToDateTime(dtEnd.ToString("yyyy-MM-dd") + " 23:59:59").ToString("yyyy-MM-dd HH:mm:ss");
            DoValueChanged?.Invoke(this, null);
        }

        public event EventHandler DoValueChanged;

        private void startDate_ValueChanged(object sender, DateTime value)
        {
            DoValueChanged?.Invoke(this, null);
        }
    }
}
